﻿using Infrastructure;
using Infrastructure.Attribute;
using Infrastructure.Extensions;
using Org.BouncyCastle.Asn1.Cms;
using Org.BouncyCastle.Ocsp;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using ZR.Model;
using ZR.Model.Dev.Entity;
using ZR.Model.Enums;
using ZR.Service.Dev.IService;
using ZR.Service.IService;
using ZR.ServiceCore;
using ZR.ServiceCore.Attributes;

namespace ZR.Service
{
    [AppService(ServiceType = typeof(IToolService), ServiceLifetime = LifeTime.Scoped)]
    public class ToolService : BaseService<MenuEntity>, IToolService
    {
        private readonly ICodeModuleInfoService _codeModuleInfoService;
        public ToolService(ICodeModuleInfoService codeModuleInfoService) 
        { 
            this._codeModuleInfoService= codeModuleInfoService;
        }

        public async Task<List<CodeModuleTreeNodeInfo>> GetCodeModuleTreeList(GetCodeModuleTreeListReq req)
        {
            List<CodeModuleTreeNodeInfo> list = this._codeModuleInfoService.CodeModuleInfos;
            
            
            var paId = req.PaModuleCode;
            if (paId == null)
            {
                if(req.MenuType==MenuTypeEnum.Catalog)
                {
                    list = list.Where(m => m.PaId == null).ToList();
                }
            }
            else
            {
                list = list.Where(m => m.PaId?.Equals(paId, StringComparison.OrdinalIgnoreCase) == true).ToList();
            }
            if (!string.IsNullOrWhiteSpace(req.MenuTypeCode))
            {
                foreach (var item in list)
                {
                    if (!item.Type.Equals(req.MenuTypeCode, StringComparison.OrdinalIgnoreCase))
                    {
                        item.IsDisabled = true;
                    }
                }
            }
            list = BuildCodeModuleTree(list);
            return list;
        }

        #region 构建前端所需要树结构

        /// <summary>
        /// 构建前端所需要树结构
        /// </summary>
        /// <param name="list">列表</param>
        /// <returns></returns>
        private List<CodeModuleTreeNodeInfo> BuildCodeModuleTree(List<CodeModuleTreeNodeInfo> list)
        {
            List<CodeModuleTreeNodeInfo> returnList = new List<CodeModuleTreeNodeInfo>();
            List<string> allIds = list.Select(f => f.Id).ToList();
            foreach (var dept in list)
            {
                // 如果是顶级节点, 遍历该父节点的所有子节点
                if (!allIds.Contains(dept.PaId))
                {
                    FillChildren(list, dept);
                    returnList.Add(dept);
                }
            }

            if (!returnList.Any())
            {
                returnList = list;
            }
            return returnList;
        }

        /// <summary>
        /// 填充子结点
        /// </summary>
        /// <param name="list"></param>
        /// <param name="t"></param>
        private void FillChildren(List<CodeModuleTreeNodeInfo> list, CodeModuleTreeNodeInfo t)
        {
            //得到子节点列表
            List<CodeModuleTreeNodeInfo> childList = GetChildList(list, t);
            t.Children = childList;
            foreach (var item in childList)
            {
                if (GetChildList(list, item).Count() > 0)
                {
                    FillChildren(list, item);
                }
            }
        }

        /// <summary>
        /// 递归获取子结点
        /// </summary>
        /// <param name="list">所有结点</param>
        /// <param name="item">结点</param>
        /// <returns></returns>
        private List<CodeModuleTreeNodeInfo> GetChildList(List<CodeModuleTreeNodeInfo> list, CodeModuleTreeNodeInfo item)
        {
            return list.Where(p => p.PaId == item.Id).ToList();
        }

        #endregion
    }


}
